@extends('layouts.application')
@section('content')
    <div class="admin-content">
        <div class="admin-content-body">
            <div class="am-cf am-padding am-padding-bottom-0">
                <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">品牌管理</strong> /
                    <small>Brand Manager</small>
                </div>
            </div>

            <hr>

            <div class="am-g">
                <div class="am-u-sm-12 am-u-md-6">
                    <div class="am-btn-toolbar">
                        <div class="am-btn-group am-btn-group-xs">
                            <a href="{{route('brands.create')}}" class="am-btn am-btn-default"><span
                                        class="am-icon-plus"></span> 新增</a>


                            <button type="button" class="am-btn am-btn-default destroy_all"><span class="am-icon-trash-o"></span> 删除
                            </button>
                        </div>
                    </div>
                </div>
                <div class="am-u-sm-12 am-u-md-3">
                    <div class="am-form-group">
                        <select data-am-selected="{btnSize: 'sm'}">
                            <option value="option1">所有类别</option>
                            <option value="option2">IT业界</option>
                            <option value="option3">数码产品</option>
                            <option value="option3">笔记本电脑</option>
                            <option value="option3">平板电脑</option>
                            <option value="option3">只能手机</option>
                            <option value="option3">超极本</option>
                        </select>
                    </div>
                </div>
                <div class="am-u-sm-12 am-u-md-3">
                    <div class="am-input-group am-input-group-sm">
                        <input type="text" class="am-form-field">
                        <span class="am-input-group-btn">
            <button class="am-btn am-btn-default" type="button">搜索</button>
          </span>
                    </div>
                </div>
            </div>

            <div class="am-g">
                <div class="am-u-sm-12">
                    <form class="am-form">
                        <table class="am-table am-table-striped am-table-hover table-main">
                            <thead>
                            <tr>
                                <th class="table-check "><input type="checkbox" class="checked_all"/></th>
                                <th class="table-id" name="sort_order" style="width: 5%;">排序</th>
                                <th class="table-title" name="name">品牌名称</th>
                                <th class="table-type" name="description">描述</th>
                                <th class="table-type" name="is_show">是否显示</th>
                                >
                                <th class="table-set">操作</th>
                            </tr>
                            </thead>

                            <tbody>
                            @foreach($brands as $brand)
                                <tr data-id="{{$brand->id}}">
                                    <td><input type="checkbox" name="checked_id[]" class="checked_id"
                                               value="{{$brand->id}}"/></td>
                                    <td><input type="text" value="{{$brand->sort_order}}" class="sort_order"></td>
                                    <td>{{$brand->name}}</td>
                                    <td>{{$brand->description}}</td>
                                    <td>
                                        @if($brand->is_show==1)
                                            <span class="am-icon-check is_show"></span>
                                        @else
                                            <span class="am-icon-close is_show"></span>
                                        @endif
                                    </td>
                                    <td>
                                        <div class="am-btn-toolbar">
                                            <div class="am-btn-group am-btn-group-xs">
                                                <button class="am-btn am-btn-default am-btn-xs am-text-secondary"><span
                                                            class="am-icon-pencil-square-o"></span> 编辑
                                                </button>
                                                <button class="am-btn am-btn-default am-btn-xs am-hide-sm-only"><span
                                                            class="am-icon-copy"></span> 复制
                                                </button>
                                                <a href="{{route('brands.destroy', $brand->id)}}" data-method="delete"
                                                   data-token=" {{csrf_token()}}" data-confirm="确定要删除吗?"
                                                   class="am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only">
                                                    <span class="am-icon-trash-o"></span> 删除
                                                </a>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                            @endforeach
                            </tbody>
                        </table>
                        <div class="am-cf">
                            共 {{$brands->total()}} 条记录
                            <div class="am-fr">
                                {{ $brands->links() }}
                            </div>
                        </div>
                        <hr/>
                    </form>
                </div>

            </div>
        </div>

        <footer class="admin-content-footer">
            <hr>
            <p class="am-padding-left">© 2014 AllMobilize, Inc. Licensed under MIT license.</p>
        </footer>

    </div>
@endsection
@section('js')
    <script>
        $(function () {
            $('.is_show').click(function () {
                var data = {id: $(this).parents('tr').data('id')};
                var _this = $(this);
                $.ajax({
                    type: "PATCH",
                    url: "{{route('brands.is_show')}}",
                    data: data,
                    //toggle 转换
                    success: function (data) {
                        _this.toggleClass("am-icon-check am-icon-close")
                    }

                })


            })
            $('.sort_order').change(function () {
                var data = {
                    id: $(this).parents('tr').data('id'),
                    sort_order: $(this).val()
                };
                $.ajax({
                    type: "PATCH",
                    url: "{{route('brands.sort_order')}}",
                    data: data,
                    error:function (data) {
                        alert(data.responseJSON.sort_order);
                    }
                })

            })
            $('.checked_all').click(function () {
                var checked =$(this).prop('checked');
                $('.checked_id').prop('checked',checked);
            })
            $('.destroy_all').click(function () {
                var length=$('.checked_id:checked').length;
                if(length==0){
                    alert('至少选中一个')
                }
                var data=$('.checked_id').serialize(); //serialize 英文翻译：连载；连播
                console.log(data);
                $.ajax({
                  type:"DELETE",
                  url:"{{route('brands.destroy_all')}}",
                  data:data,
                  success:function (data) {
                      location.href=location.href
                  }
                })
            })

        })

    </script>
@endsection